package com.love.mview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    public static final int HEIGHT = 1000;
    private View view_line;
    private ImageView img_icon;
    private marc mMarc;
    private Timer mTimer;
    private int i;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 100:
                    i += 5;
                    if (i <= 360) {
                        mMarc.setValue(i);
                    } else {
                        if (mTimer != null) {
                            mTimer.cancel();
                            mTimer = null;
                        }
                    }
                    break;
                case 102:
                    if (mTimer != null) {
                        mTimer.cancel();
                        mTimer = null;
                    }
                    break;
            }
        }
    };
    private LinearLayout lay_conten;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                startActivity(intent);
            }
        });


        lay_conten = (LinearLayout) findViewById(R.id.lay_conten);
        img_icon = (ImageView) findViewById(R.id.img_icon);
        view_line = findViewById(R.id.view_line);

        mMarc = (marc) findViewById(R.id.marc);


        ConfigurationView view = new ConfigurationView(this, 100, "温度/℃", false, false);
        lay_conten.addView(view, new LinearLayout.LayoutParams(50, LinearLayout.LayoutParams.FILL_PARENT));

        ConfigurationView view1 = new ConfigurationView(this, 1000, "电量/%", true);
        lay_conten.addView(view1, new LinearLayout.LayoutParams(50, LinearLayout.LayoutParams.FILL_PARENT));

        ConfigurationView view2 = new ConfigurationView(this, 80, "电量/%", true);
        lay_conten.addView(view2, new LinearLayout.LayoutParams(50, LinearLayout.LayoutParams.FILL_PARENT));

        ConfigurationView view3 = new ConfigurationView(this, 40, "电量/%", true);
        lay_conten.addView(view3, new LinearLayout.LayoutParams(50, LinearLayout.LayoutParams.FILL_PARENT));

        ConfigurationView view4 = new ConfigurationView(this, 2, "电量/%", true);
        lay_conten.addView(view4, new LinearLayout.LayoutParams(50, LinearLayout.LayoutParams.FILL_PARENT));


        rotation();
        img_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
                startTime();
            }
        });
    }


    private void startTime() {
        if (mTimer == null) {
            mTimer = new Timer();
        }
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(100);
            }
        }, 0, 200);
    }

    private void start() {
        ObjectAnimator animatorIcon = ObjectAnimator.ofFloat(img_icon, "translationY", 0f, 100f, 0f);
        ObjectAnimator animatorLine = ObjectAnimator.ofFloat(view_line, "scaleY", 1.0f, 5.0f, 1.0f);
        animatorLine.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Toast.makeText(MainActivity.this, "end", Toast.LENGTH_SHORT).show();
                mHandler.sendEmptyMessage(102);

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(animatorIcon).with(animatorLine);
        animatorSet.setDuration(2000);
        animatorSet.start();
    }


    private void rotation() {
        final ObjectAnimator animator = ObjectAnimator.ofFloat(img_icon, "rotation", 0, 10, 0, -10);
        animator.setRepeatCount(Integer.MAX_VALUE);
        animator.setRepeatMode(ValueAnimator.REVERSE);


        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animator.setupStartValues();
            }
        });

        img_icon.setPivotY(0);
        img_icon.setPivotX(getResources().getDrawable(R.mipmap.home_redbag).getIntrinsicWidth() / 2);
        img_icon.invalidate();
        AnimatorSet set = new AnimatorSet();
        set.play(animator);
        set.setDuration(1000);
        set.setInterpolator(new LinearInterpolator());
        set.start();
    }
}
